import http from 'luban-http'
import Vue from 'vue'
import NProgress from 'nprogress'

const defaultOptions = {
  timeout: 30000, // 设置超时时长
  baseURL: '', // 设置baseURL
  requestInterceptor: config => {
    NProgress.start()
  }, //发送请求成功的回调
  requestInterceptorError: error => {
    NProgress.done()
  }, // 发送请求失败的回调
  responseInterceptor: response => {
    NProgress.done()
  }, // 请求响应成功的回调
  responseInterceptorError: error => {
    NProgress.done()
  }, // 请求响应失败的回调
  commonErrorHandling: (message, res) => {
    Vue.prototype.$message &&
      Vue.prototype.$message.error(message || '未知异常')
  } // 统一错误处理的回调
}

export const setOptions = (httpOptions = {}) => {
  const options = { ...defaultOptions, ...httpOptions }
  http.setOptions(options)
}
